package com.sun.filesigndemo.common.utils;

import com.sun.filesigndemo.common.conf.Global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

public class UploadFileUtil {

	static Logger logger = LoggerFactory.getLogger(UploadFileUtil.class);

	public final static String ORIGINAL_DIRECTORY= "ori"; 
	public final static String THUMBNAIL_DIRECTORY= "min";
	private final static Boolean IS_DELETE_LOCAL_FILE = Global.getBoolean("app.qiniu.delLocal");
	
	/**
	 * 存放根目录：/USER_HOME/file/projectNpame
	 */
	public final static String UPLOAD_DRIECTORY= String.format("%s%2$sfiles%2$s%3$s", System.getProperty("user.home"),
			File.separator, Global.get("projectName"));

	/**
	 * 上传图片到7牛，之后删除服务器图片
	 * @param request
	 * @param file
	 * @param subDirectory
	 * @param wide
	 * @param height
	 * @param del上传到7牛后是否删除本地文件
	 * @return
	 */
	public static FileVo qiniuUpload(HttpServletRequest request, MultipartFile file, String subDirectory) {
		FileVo fileVo= fileUpload(request, file, subDirectory);
		Boolean uploadState = QiniuUpload.upload(fileVo.getOriginal(), subDirectory + File.separator + fileVo.getFileKey());
		if (IS_DELETE_LOCAL_FILE) {
			clean(fileVo);
		}
		if (!uploadState) {
			fileVo.setState(false);
			return fileVo;
		}
		fileVo.setQiniuUrl(String.format("/%s/%s", subDirectory, fileVo.getFileKey()));
		return fileVo;
	}
	
	/**
	 * 上传压缩图片到7牛，之后删除服务器图片
	 * @param request
	 * @param file
	 * @param subDirectory
	 * @param wide
	 * @param height
	 * @param del上传到7牛后是否删除本地文件
	 * @return
	 */
	public static FileVo qiniuUpload(HttpServletRequest request, MultipartFile file, String subDirectory, Float f) {
		FileVo fileVo= fileUpload(request, file, subDirectory, f);
		Boolean uploadState = QiniuUpload.upload(fileVo.getOriginal(), subDirectory + File.separator + fileVo.getFileKey());
		if (IS_DELETE_LOCAL_FILE) {
			clean(fileVo);
		}
		if (!uploadState) {
			fileVo.setState(false);
			return fileVo;
		}
		fileVo.setQiniuUrl(String.format("/%s/%s", subDirectory, fileVo.getFileKey()));
		return fileVo;
	}


	/**
	 * 上传二维码到7牛服务器
	 * @param file
	 * @param remoteUrl
	 * @return
	 */
	public static FileVo qiniuUploadQRCode(String file, String remoteUrl) {
		FileVo fileVo= new FileVo();
		Boolean uploadState = QiniuUpload.upload(file, remoteUrl);
		fileVo.setState(uploadState);
		if (IS_DELETE_LOCAL_FILE) {
			clean(fileVo);
		}
		if (!uploadState) {
			return fileVo;
		}
		fileVo.setQiniuUrl(String.format("/%s", remoteUrl));
		return fileVo;
	}
	
	/**
	 * 上传图片到7牛，之后删除服务器图片
	 * @param request
	 * @param file
	 * @param subDirectory
	 * @param wide
	 * @param height
	 * @param del上传到7牛后是否删除本地文件
	 * @return
	 */
	public static FileVo qiniuUpload(HttpServletRequest request, MultipartFile file
			, String subDirectory, Integer wide, Integer height) {
		FileVo fileVo= fileUpload(request, file, subDirectory, wide, height);
		if (!fileVo.getState()) {
			return fileVo;
		}
		Boolean uploadState = QiniuUpload.upload(fileVo.getThumbnail(), subDirectory + File.separator + fileVo.getFileKey());
		if (IS_DELETE_LOCAL_FILE) {
			clean(fileVo);
		}
		if (!uploadState) {
			fileVo.setState(false);
			return fileVo;
		}
		fileVo.setQiniuUrl(String.format("/%s/%s", subDirectory, fileVo.getFileKey()));
		return fileVo;
	}

	/**
	 * 根据图片宽度，按比例缩放
	 * @param request
	 * @param file
	 * @param subDirectory
	 * @param f
	 * @return
	 */
	public static FileVo qiniuUploadByWidth(HttpServletRequest request, MultipartFile file
			, String subDirectory, Float f) {
		FileVo fileVo= fileUploadByWidth(request, file, subDirectory, f);
		if (!fileVo.getState()) {
			return fileVo;
		}
		Boolean uploadState = QiniuUpload.upload(fileVo.getThumbnail(), subDirectory + File.separator + fileVo.getFileKey());
		if (IS_DELETE_LOCAL_FILE) {
			clean(fileVo);
		}
		if (!uploadState) {
			fileVo.setState(false);
			return fileVo;
		}
		fileVo.setQiniuUrl(String.format("/%s/%s", subDirectory, fileVo.getFileKey()));
		return fileVo;
	}
	
	public static Boolean clean(FileVo fileVo) {
		FileUtils.deleteFile(fileVo.getOriginal());
		FileUtils.deleteFile(fileVo.getThumbnail());
		return true;
	}
	
	/**
	 * 上传图片
	 * @param request 请求
	 * @param file 请求文件
	 * @param subDirectory 子目录，相对目录
	 * @return
	 */
	public static FileVo fileUpload(HttpServletRequest request, MultipartFile file, String subDirectory) {
		if (file == null) {
			return null;
		}
		
		FileVo fileVo= new FileVo();
		// 原文件名
		fileVo.setOriginalName(file.getOriginalFilename());
		fileVo.setSuffix(FileUtils.getFileExtension(file.getOriginalFilename()));
		fileVo.setFileKey(IdGen.uuid());
		fileVo.setDirectory(String.format("%s%s%s", UPLOAD_DRIECTORY, File.separator, subDirectory));

		// 检查创建右上方目录
		FileUtils.createDirectory(fileVo.getOriginalDrictory());
		// 文件保存路径
		File oriFile= new File(fileVo.getOriginal());
		
		try {
			// 转存文件
			file.transferTo(oriFile);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return fileVo;
		}
		fileVo.setState(true);
		return fileVo;
	}

	/**
	 * 指定宽高上传图片
	 * @param request
	 * @param file
	 * @param subDirectory
	 * @param wide
	 * @param height
	 * @return
	 */
	public static FileVo fileUpload(HttpServletRequest request, MultipartFile file, String subDirectory, Integer wide, Integer height) {

		FileVo fileVo = fileUpload(request, file, subDirectory);
		if (!fileVo.getState()) {
			return fileVo;
		}
		
		File oriFile= new File(fileVo.getOriginal());
		
		/*
		 * 缩略图
		 */
		FileUtils.createDirectory(fileVo.getThumbnailDrictory());
		ImageUtil.changeImge(oriFile, wide, height, fileVo.getThumbnail());
		return fileVo;
	}

	/**
	 * 指定比例上传图片
	 * @param request
	 * @param file
	 * @param subDirectory
	 * @param wide
	 * @param height
	 * @return
	 */
	public static FileVo fileUploadByWidth(HttpServletRequest request, MultipartFile file, String subDirectory, Float f) {

		FileVo fileVo = fileUpload(request, file, subDirectory);
		if (!fileVo.getState()) {
			return fileVo;
		}

		File oriFile= new File(fileVo.getOriginal());

		/*
		 * 缩略图
		 */
		FileUtils.createDirectory(fileVo.getThumbnailDrictory());
		ImageUtil.changeImge(oriFile, f, fileVo.getThumbnail());
		return fileVo;
	}
	
	/**
	 * 指定压缩比率上传图片
	 * @param request
	 * @param file
	 * @param subDirectory
	 * @param wide
	 * @param height
	 * @return
	 */
	public static FileVo fileUpload(HttpServletRequest request, MultipartFile file, String subDirectory, Float f) {

		FileVo fileVo = fileUpload(request, file, subDirectory);
		if (!fileVo.getState()) {
			return fileVo;
		}
		
		File oriFile= new File(fileVo.getOriginal());
		
		/*
		 * 压缩
		 */
		FileUtils.createDirectory(fileVo.getThumbnailDrictory());
		ImageUtil.outputQuality(oriFile, f);
		return fileVo;
	}

	public static void main(String[] args) {
//		System.out.println(String.format("%s%2$sfiles%2$s%3$s%2$s%4$s%2$sOri", System.getProperty("user.home"),
//				File.separator, "dl" , "avater"));
	}
	
	public static class FileVo 
	{
		/**
		 * 不包括后缀的文件名
		 */
		String fileKey;
		/**
		 * 文件后缀
		 */
		String suffix;
		/**
		 * 原图全路径
		 */
		String original;
		/**
		 * 缩略图
		 */
		String thumbnail;
		/**
		 * 文件名
		 */
		String name;
		/**
		 * 原文件名
		 */
		String originalName;
		/**
		 * 存放目录
		 */
		String directory;

		/**
		 * 原图目录
		 */
		String originalDrictory;
		

		/**
		 * 缩略图目录
		 */
		String thumbnailDrictory;
		
		Boolean state;
		
		String qiniuUrl;
		
		public FileVo() {}
		
		/**
		 * 不包括后缀的文件名
		 */
		public String getFileKey() {
			return fileKey;
		}
		/**
		 * 不包括后缀的文件名
		 */
		public void setFileKey(String fileKey) {
			this.fileKey = fileKey;
		}

		/**
		 * 文件后缀
		 */
		public String getSuffix() {
			return suffix;
		}

		/**
		 * 文件后缀
		 */
		public void setSuffix(String suffix) {
			this.suffix = suffix;
		}

		/**
		 * 原图全路径
		 */
		public String getOriginal() {
			return String.format("%s%s%s", getOriginalDrictory(), File.separator, getName());
		}

		/**
		 * 缩略图
		 */
		public String getThumbnail() {
			return String.format("%s%s%s", getThumbnailDrictory(), File.separator, getName());
		}

		/**
		 * 右上方目录
		 */
		public String getDirectory() {
			return directory;
		}

		/**
		 * 右上方目录
		 */
		public void setDirectory(String directory) {
			this.directory = directory;
		}

		/**
		 * 文件名
		 */
		public void setName(String name) {
			String[] ns= name.split("\\.");
			System.out.println(ns.length);
			if (ns.length < 2) {
				this.fileKey= name;
				return;
			}

			this.fileKey= ns[0];
			this.suffix= ns[1];
		}

		/**
		 * 文件名
		 */
		public String getName() {
			return String.format("%s.%s", fileKey, suffix);
		}

		public String getOriginalName() {
			return originalName;
		}

		public void setOriginalName(String originalName) {
			this.originalName = originalName;
		}

		public String getOriginalDrictory() {
			return String.format("%s%s%s", directory, File.separator, ORIGINAL_DIRECTORY);
		}

		public String getThumbnailDrictory() {
			return String.format("%s%s%s", directory, File.separator, THUMBNAIL_DIRECTORY);
		}

		public Boolean getState() {
			return state;
		}

		public void setState(Boolean state) {
			this.state = state;
		}

		public String getQiniuUrl() {
			return qiniuUrl;
		}

		public void setQiniuUrl(String qiniuUrl) {
			this.qiniuUrl = qiniuUrl;
		}

		public String getOriPath() {
			return String.format("/%s%s%s", ORIGINAL_DIRECTORY, File.separator, getName());
		}
		
	}
}
